package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ParkingRate;
import com.ruoyi.system.service.IParkingRateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 停车费Controller
 *
 * @author ruoyi
 * @date 2021-06-28
 */
@RestController
@RequestMapping("/system/rate")
public class ParkingRateController extends BaseController {
    @Autowired
    private IParkingRateService parkingRateService;

    /**
     * 查询停车费列表
     */
//    @PreAuthorize("@ss.hasPermi('system:rate:list')")
    @GetMapping("/list")
    public TableDataInfo list(ParkingRate parkingRate) {
        startPage();
        List<ParkingRate> list = parkingRateService.selectParkingRateList(parkingRate);
        return getDataTable(list);
    }

    @GetMapping("/Daylist")
    public List Daylist(ParkingRate parkingRate) {
        return parkingRateService.ParkingRateList(parkingRate);
    }

    /**
     * 导出停车费列表
     */
//    @PreAuthorize("@ss.hasPermi('system:rate:export')")
    @Log(title = "停车费", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ParkingRate parkingRate) {
        List<ParkingRate> list = parkingRateService.selectParkingRateList(parkingRate);
        ExcelUtil<ParkingRate> util = new ExcelUtil<ParkingRate>(ParkingRate.class);
        return util.exportExcel(list, "rate");
    }

    /**
     * 获取停车费详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:rate:query')")
    @GetMapping(value = "/{prId}")
    public AjaxResult getInfo(@PathVariable("prId") Long prId) {
        return AjaxResult.success(parkingRateService.selectParkingRateById(prId));
    }

    /**
     * 新增停车费
     */
//    @PreAuthorize("@ss.hasPermi('system:rate:add')")
    @Log(title = "停车费", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParkingRate parkingRate) { // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("YYYY-mm-dd HH:mm:ss");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        // 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(parkingRate.getPrEndtime().toString()).getTime() - sd.parse(parkingRate.getPrStarttime().toString()).getTime();
            hour = diff % nd / nh + day * 24;// 计算差多少小时
            // 输出结果
            System.out.println("hour=" + hour);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return toAjax(parkingRateService.insertParkingRate(parkingRate));
    }

    /**
     * 修改停车费
     */
//    @PreAuthorize("@ss.hasPermi('system:rate:edit')")
    @Log(title = "停车费", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParkingRate parkingRate) {
        return toAjax(parkingRateService.updateParkingRate(parkingRate));
    }

    /**
     * 删除停车费
     */
//    @PreAuthorize("@ss.hasPermi('system:rate:remove')")
    @Log(title = "停车费", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prIds}")
    public AjaxResult remove(@PathVariable Long[] prIds) {
        return toAjax(parkingRateService.deleteParkingRateByIds(prIds));
    }
}
